package com.prs.crm.action.stock.purchase;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.Address;
import com.prs.crm.domain.stock.Supplier;
import com.prs.crm.domain.stock.SupplierType;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.stock.SupplierService;
import com.prs.crm.service.stock.SupplierTypeService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class SupplierAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Supplier supplier;
	private Integer[] supplierIds;

	public Integer[] getSupplierIds() {
		return supplierIds;
	}

	public void setSupplierIds(Integer[] supplierIds) {
		this.supplierIds = supplierIds;
	}

	private String tel;

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	private String email;
	private String phone;
	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	@Autowired
	private SupplierService supplierservice;
	private PaginationSupport pagination;
	private File upload;
	private String uploadFileName;
	private List<SupplierType> types;
	private Integer[] supplierids;
	private SupplierTypeService supplierTypeService;
	private Collection<CodeCommon> codelist;

	@Action("listSupplier")
	public String listsupplier() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Supplier.class);
		this.setPagination(this.getSupplierservice().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("listSupplierAlter")
	public String listSupplierAlter() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Supplier.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSupplierservice().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveSupplier", results = {
			@Result(name = "success", type = "redirect", location = "editSupplier"),
			@Result(name = "none", type = "redirect", location = "editSupplier"),
			@Result(name = "error", type = "chain", location = "editSupplier") })
	public String savesuppliter() {

		if (supplier.getName() == null || supplier.getName().equals("")) {
			this.addActionError("名称不能为空");
			return ERROR;

		}

		if (supplier.getType() == null) {
			this.addActionError("类型不能为空");
			return ERROR;
		}
		if (supplier.getClerk() == null) {
			this.addActionError("业务员不能为空");
			return ERROR;
		}
		if (supplier.getDistrict() == null) {
			this.addActionError("地区不能为空");
			return ERROR;
		}
		if (supplier.getSupplierCode() == null
				|| supplier.getSupplierCode().equals("")) {
			this.addActionError("编码不能为空");
			return ERROR;

		}
		
		if (isnull(supplier.getTel())) {
			if (istel(supplier.getTel())) {
				this.addActionError("电话格式不对！请重新输入");
				return ERROR;
			}
		}
		if (isnull(supplier.getPhone()))
			if (isphone(supplier.getPhone())) {
				this.addActionError("手机号码格式有错!请重新输入");
				return ERROR;
			}
		if (isnull(supplier.getEmail())) {
			if (emailFormat(supplier.getEmail())) {
				this.addActionError("邮箱格式不对!请重新输入");
				return ERROR;
			}
		}
		if ("x".equals(Encode) || "".equals(Encode)) {
			this.addActionError("请选择编码类型");
			return ERROR;
		}
		if (supplier.getId() != null) {

		} else {
			if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll(
						"手动编码", "供应商", supplier.getSupplierCode());
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(supplier.getSupplierCode());
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("供应商");
				this.getCodeService().save(codes);
				supplier.setCode(codes);

			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(supplier.getSupplierCode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("供应商");
				this.getCodeService().save(codes);
				supplier.setCode(codes);

			}
		}
		this.getSupplierservice().save(supplier);
		this.setPrompt("操作成功");
		/**
		 * 编码产生
		 */
		return SUCCESS;

	}

	@Action(value = "removeSupplier", results = {
			@Result(name = "success", type = "redirect", location = "listSupplierAlter"),
			@Result(name = "error", type = "chain", location = "alterSupplier") })
	public String removeSupplier() {
		this.getSupplierservice().batchRemove(supplierIds);
		return SUCCESS;
	}

	@Action("editSupplier")
	public String edit() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"供应商");
		this.types = this.getSupplierTypeService().getAllTypes();
		return "SUCCESS";
	}

	@Action("viewSupplier")
	public String viewSupplier() {
		supplier = this.getSupplierservice().get(supplier.getId());
		return SUCCESS;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public SupplierTypeService getSupplierTypeService() {
		return supplierTypeService;
	}

	public void setSupplierTypeService(SupplierTypeService supplierTypeService) {
		this.supplierTypeService = supplierTypeService;
	}

	public List<SupplierType> getTypes() {
		return types;
	}

	public void setTypes(List<SupplierType> types) {
		this.types = types;
	}

	public Integer[] getSupplierids() {
		return supplierids;
	}

	public void setSupplierids(Integer[] supplierids) {
		this.supplierids = supplierids;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public SupplierService getSupplierservice() {
		return supplierservice;
	}

	public void setSupplierservice(SupplierService supplierservice) {
		this.supplierservice = supplierservice;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public boolean emailFormat(String email) {
		boolean tag = false;
		final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(email);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean istel(String str) {
		boolean tag = false;  
		final String pattern1 = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean isphone(String str) {
		boolean tag = false;
		final String pattern1 = "1+[3|5|8]{1}+[0-9]{9}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean isnull(String str) {
		if (str.equals("") || str == null) {
			return false;
		}
		return true;
	}
}
